"""
模型序列化
定义DRF 框架的序列化类
"""

from rest_framework import serializers
from app.models import User, UserData, DisordersType, Questionnaire, QuestionItem, ScoreResult, EmgData, \
    LiteratureKnowledge, MagneticStimulusPrescriptions, ElectricalStimulusPrescriptions, PatientCase


class ScoreResultSerializer(serializers.ModelSerializer):
    user_data_id = serializers.CharField(max_length=20)
    question_result = serializers.CharField(max_length=255)
    disorders_type = serializers.CharField(max_length=4)
    total_score = serializers.CharField(required=False,allow_null=True,max_length=10)
    total_score_result = serializers.CharField(required=False,allow_null=True,max_length=255)
    class Meta:
        model = ScoreResult
        fields = "__all__"


class QuestionItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = QuestionItem
        fields = "__all__"

class QuestionnaireSerializer(serializers.ModelSerializer):
    class Meta:
        model = Questionnaire
        fields = "__all__"

class UserSerializer(serializers.ModelSerializer):
    """用户模型序列化"""
    class Meta:
        model = User
        fields = ['id','username','email','mobile']

class DisordersTypeSerializer(serializers.ModelSerializer):
    """用户模型序列化"""
    class Meta:
        model = DisordersType
        fields = '__all__'

class LiteratureKnowledgeSerializer(serializers.ModelSerializer):
    two_type = serializers.CharField(max_length=20)
    symptoms = serializers.CharField(max_length=20)
    contraindications = serializers.CharField(max_length=20)
    prescription_type = serializers.CharField(max_length=20)
    details = serializers.CharField(max_length=255)
    class Meta:
        model = LiteratureKnowledge
        fields = '__all__'

class MagneticStimulusPrescriptionsSerializer(serializers.ModelSerializer):
    indication = serializers.CharField(max_length=50)
    protocol_name = serializers.CharField(max_length=100)
    stimulation_site = serializers.CharField(max_length=50)
    frequency = serializers.CharField(max_length=20)
    stimulation_duration = serializers.CharField(max_length=20)
    interval_duration = serializers.CharField(max_length=20)
    number_of_series = serializers.CharField(max_length=20)
    rest_duration = serializers.CharField(max_length=20)
    class Meta:
        model = MagneticStimulusPrescriptions
        fields = '__all__'

class ElectricalStimulusPrescriptionsSerializer(serializers.ModelSerializer):
    category = serializers.CharField(max_length=50)
    name = serializers.CharField(max_length=50)
    stimulation_time = serializers.CharField(max_length=20)
    rest_time = serializers.CharField(max_length=20)
    rise_time = serializers.CharField(max_length=20)
    fall_time = serializers.CharField(max_length=20)
    stimulation_frequency = serializers.CharField(max_length=20)
    pulse_width = serializers.CharField(max_length=20)
    class Meta:
        model = ElectricalStimulusPrescriptions
        fields = '__all__'

class EmgDataSerializer(serializers.ModelSerializer):
    user_data_id = serializers.CharField(max_length=20)
    average_emg = serializers.CharField(required=False,allow_null=True,max_length=20)
    median_frequency = serializers.CharField(required=False,allow_null=True,max_length=20)
    variation_coefficient = serializers.CharField(required=False,allow_null=True,max_length=20)
    up_down_time = serializers.CharField(required=False,allow_null=True,max_length=20)
    class Meta:
        model = EmgData
        fields = '__all__'

class UserDataSerializer(serializers.Serializer):
    id = serializers.CharField(required=False,allow_null=True,max_length=50)
    medical_record_num = serializers.CharField(max_length=50)
    user_name = serializers.CharField(max_length=16)
    age = serializers.CharField(required=False,allow_null=True,max_length=6)
    gender = serializers.CharField(required=False,allow_null=True,max_length=4)
    height = serializers.CharField(required=False,allow_null=True,max_length=6)
    weight = serializers.CharField(required=False,allow_null=True,max_length=6)
    pulse = serializers.CharField(required=False,allow_null=True,max_length=6)
    blood_pressure = serializers.CharField(required=False,allow_null=True,max_length=6)
    start_time = serializers.CharField(required=False,allow_null=True,max_length=16)
    disorders_type = serializers.CharField(max_length=10)
    allergy_history = serializers.CharField(required=False,allow_null=True,max_length=255)
    past_history = serializers.CharField(required=False,allow_null=True,max_length=255)
    hpi = serializers.CharField(required=False,allow_null=True,max_length=255)
    user_id = serializers.CharField(required=False,allow_null=True, max_length=20)
    class Meta:
        model = UserData
        fields = '__all__'

    def create(self, validated_data):
        return UserData.objects.create(**validated_data)


class PatientCaseSerializer(serializers.ModelSerializer):

    class Meta:
        model = PatientCase
        fields = '__all__'

    def create(self, validated_data):
        return PatientCase.objects.create(**validated_data)